home *** CD-ROM | disk | FTP | other *** search
/ ftp.qualcomm.com / 2014.06.ftp.qualcomm.com.tar / ftp.qualcomm.com / eudora / developers / emsapi / carbon_emsapi.sit.hqx / Macintosh API Support / buftype.c next >
Text File  |  2001-03-08  |  10KB  |  494 lines

  1. /* ======================================================================
  2.  
  3.     Functions to manage string buffers
  4.  
  5.     Filename:            buftype.c
  6.     Last Edited:        March 7, 1997
  7.     Authors:            Scott Manjourides, Laurence Lundblade, Bob Fronabarger
  8.     Copyright:            1995, 1996 QUALCOMM Inc.
  9.     Technical support:    <emsapi-info@qualcomm.com>
  10. */
  11.  
  12. #include <string.h>
  13. #include "buftype.h"
  14.  
  15.  
  16. static Boolean ValidBuf(BufTypeHandle bh);
  17. static Boolean ResizeBuf_Buf(BufTypeHandle dst, unsigned long newSize);
  18.  
  19. //-------------------------------------------------------
  20.  
  21. Boolean ValidBuf(BufTypeHandle bh)
  22. {
  23.     if (!bh)
  24.         return false;
  25.     return ((**bh).bufHandle != nil);
  26. }
  27.  
  28. //-------------------------------------------------------
  29.  
  30. BufTypeHandle MakeSize_Buf(unsigned long n)
  31. {
  32.     BufTypeHandle    bh;
  33.     
  34.     bh = (BufTypeHandle) NewHandleClear(sizeof(BufType));
  35.     if (!bh)
  36.         return nil;
  37.  
  38.     (**bh).bufHandle = NewHandleClear(n);
  39.     if (!(**bh).bufHandle) {
  40.         Delete_Buf(bh);
  41.         bh = nil;
  42.     }
  43.     return bh;
  44. }
  45.  
  46. //-------------------------------------------------------
  47.  
  48. BufTypeHandle Make_Buf(void)
  49. {
  50.     return (BufTypeHandle) NewHandleClear(sizeof(BufType));
  51. }
  52.  
  53. //-------------------------------------------------------
  54.  
  55. Boolean IsEmpty_Buf(BufTypeHandle bh)
  56. {
  57.     if (!ValidBuf(bh))
  58.         return true;
  59.     return ((**bh).len == 0);
  60. }
  61.  
  62. //-------------------------------------------------------
  63.  
  64. unsigned long BufLen_Buf(BufTypeHandle bh) /* Number of valid chars in buffer */
  65. {
  66.     return (IsEmpty_Buf(bh) ? 0 : (**bh).len);
  67. }
  68.  
  69. //-------------------------------------------------------
  70.  
  71. Handle GetBuf_Buf(BufTypeHandle bh)
  72. {
  73.     return (bh ? (**bh).bufHandle : nil);
  74. }
  75.  
  76. //-------------------------------------------------------
  77.  
  78. Ptr GetEnd_Buf(BufTypeHandle bh)
  79. {
  80.     Handle        h;
  81.     char        *cp = nil;
  82.     
  83.     h = GetBuf_Buf(bh);
  84.     if (h)
  85.         cp = (*h + (**bh).len - 1);
  86.     return cp;
  87. }
  88.  
  89. //-------------------------------------------------------
  90.  
  91. unsigned long BufSize_Buf(BufTypeHandle bh)
  92. {
  93.     return (ValidBuf(bh) ? GetHandleSize((**bh).bufHandle) : 0);
  94. }
  95.  
  96. //-------------------------------------------------------
  97.  
  98. Boolean SetLen_Buf(BufTypeHandle bh, unsigned long n)
  99. {
  100.     if (!ValidBuf(bh) || (n > GetHandleSize((**bh).bufHandle)))
  101.         return false;
  102.  
  103.     (**bh).len = n;
  104.     return true;
  105. }
  106.  
  107. #pragma mark -
  108. //-------------------------------------------------------
  109.  
  110. unsigned long BufNCat_Buf(BufTypeHandle dst, BufTypeHandle src, unsigned long n)
  111. {
  112.     unsigned long    oldSize, addSize, newSize;
  113.     char            *dstStart;
  114.  
  115.     if ((IsEmpty_Buf(src)) || (n < 1) || !dst)
  116.         return 0;
  117.  
  118.     oldSize = BufLen_Buf(dst);
  119.     addSize = PosLen_Buf(src);
  120.     if (n < addSize)
  121.         addSize = n;
  122.  
  123.     newSize = oldSize + addSize;
  124.     if (newSize > BufSize_Buf(dst)) {
  125.         if (!ResizeBuf_Buf(dst, newSize))
  126.             return 0;
  127.     }
  128.     if (oldSize == 0) {
  129.         (**dst).pos = 0;
  130.     }
  131.     if (oldSize > 0)
  132.         dstStart = GetEnd_Buf(dst) + 1;
  133.     else
  134.         dstStart = GetPos_Buf(dst);
  135.  
  136.     BlockMoveData(GetPos_Buf(src), dstStart, addSize);
  137.  
  138.     IncPos_Buf(src, addSize);
  139.     (**dst).len = newSize;
  140.  
  141.     return addSize;
  142. }
  143.  
  144. //-------------------------------------------------------
  145.  
  146. unsigned long BufIns_Buf(BufTypeHandle dst, BufTypeHandle src)
  147. {
  148.     unsigned long    oldLen, bufSize, addLen, newLen;
  149.  
  150.     if (IsEmpty_Buf(src))
  151.         return 0;
  152.  
  153.     oldLen = PosLen_Buf(dst);
  154.     bufSize = BufSize_Buf(dst);
  155.     addLen = PosLen_Buf(src);
  156.     newLen = oldLen + addLen;
  157.     if (newLen > bufSize)
  158.         addLen = bufSize - oldLen;
  159.  
  160.     if (addLen < 1)
  161.         return 0; /* Buffer is already full */
  162.  
  163.     return BufNCat_Buf(dst, src, addLen);
  164. }
  165.  
  166. //-------------------------------------------------------
  167.  
  168. unsigned long BufNIns_Buf(BufTypeHandle dst, BufTypeHandle src, unsigned long n)
  169. {
  170.     unsigned long    oldLen, bufSize, addLen, newLen;
  171.  
  172.     if ((IsEmpty_Buf(src)) || (n < 1))
  173.         return 0;
  174.  
  175.     oldLen = PosLen_Buf(dst);
  176.     bufSize = BufSize_Buf(dst);
  177.     addLen = PosLen_Buf(src);
  178.     newLen = oldLen + addLen;
  179.  
  180.     if (newLen > bufSize)
  181.         addLen = bufSize - oldLen;
  182.  
  183.     if (n < addLen)
  184.         addLen = n;
  185.  
  186.     if (addLen < 1)
  187.         return (0); /* Buffer is already full */
  188.  
  189.     return (BufNCat_Buf(dst, src, addLen));
  190. }
  191.  
  192. //-------------------------------------------------------
  193.  
  194. unsigned long StrCpy_Buf(BufTypeHandle dst, const StringPtr src)
  195. {
  196.     unsigned long    nLen;
  197.  
  198.     if ((!src) || (!dst))
  199.         return (0);
  200.  
  201.     nLen = *src;
  202.     if (nLen > 0) {
  203.         if (nLen > BufSize_Buf(dst)) {
  204.             if (!ResizeBuf_Buf(dst, nLen))
  205.                 return 0;
  206.         }
  207.         BlockMoveData(src+1, *((**dst).bufHandle), nLen);
  208.         (**dst).pos = nLen;
  209.     }
  210.     (**dst).len = (**dst).pos = nLen;
  211.     return nLen;
  212. }
  213.  
  214. //-------------------------------------------------------
  215.  
  216. unsigned long StrCat_Buf(BufTypeHandle dst, const char *src)
  217. {
  218.     unsigned long    oldSize, addSize, newSize, nCopy;
  219.     char            *cpDst, *cpSrc;    //, *cp;
  220.  
  221.     if (!src || !dst)
  222.         return 0;
  223.  
  224.     oldSize = BufLen_Buf(dst);
  225.     addSize = strlen(src);
  226.     newSize = oldSize + addSize;
  227.     if (newSize > BufSize_Buf(dst)) {
  228.         if (!ResizeBuf_Buf(dst, newSize))
  229.             return 0;
  230.     }
  231.     if (oldSize == 0)
  232.         (**dst).pos = 0;
  233.  
  234.     cpSrc = (char*) src;
  235.     if (oldSize > 0)
  236.         cpDst = GetEnd_Buf(dst) + 1;
  237.     else
  238.         cpDst = GetPos_Buf(dst);
  239.  
  240.     nCopy = addSize;
  241.     while (nCopy--)
  242.         *cpDst++ = *cpSrc++;
  243.     (**dst).len = newSize;
  244.     return addSize;
  245. }
  246.  
  247. //-------------------------------------------------------
  248.  
  249. unsigned long HandleCat_Buf(BufTypeHandle dst, const Handle src)
  250. {
  251.     unsigned long    oldSize, addSize, newSize;
  252.     char            *dstStart;
  253.  
  254.     if ((!src) || (!dst))
  255.         return 0;
  256.  
  257.     oldSize = BufLen_Buf(dst);
  258.     addSize = GetHandleSize(src);
  259.     newSize = oldSize + addSize;
  260.  
  261.     if (newSize > BufSize_Buf(dst)) {
  262.         if (!ResizeBuf_Buf(dst, newSize))
  263.             return 0;
  264.     }
  265.     if (oldSize == 0)
  266.         (**dst).pos = 0;
  267.  
  268.     if (oldSize > 0)
  269.         dstStart = GetEnd_Buf(dst) + 1;
  270.     else
  271.         dstStart = GetPos_Buf(dst);
  272.  
  273.     BlockMoveData(*src, dstStart, addSize);
  274.     (**dst).len = (**dst).pos = newSize;
  275.     return addSize;
  276. }
  277.  
  278. #pragma mark -
  279. //-------------------------------------------------------
  280.  
  281. unsigned long PosLen_Buf(BufTypeHandle bh)
  282. {
  283.     if (IsEmpty_Buf(bh))
  284.         return 0;
  285.  
  286.     return ((**bh).len - (**bh).pos);
  287. }
  288.  
  289. //-------------------------------------------------------
  290.  
  291. void IncPos_Buf(BufTypeHandle bh, unsigned long n)
  292. {
  293.     if (!ValidBuf(bh))
  294.         return;
  295.  
  296.     if (PosLen_Buf(bh) >= n)
  297.         (**bh).pos += n;
  298.     else
  299.         (**bh).pos = 0;
  300. }
  301.  
  302. //-------------------------------------------------------
  303.  
  304. void ResetPos_Buf(BufTypeHandle bh)
  305. {
  306.     if (bh)
  307.         (**bh).pos = 0;
  308. }
  309.  
  310. //-------------------------------------------------------
  311.  
  312. Ptr GetPos_Buf(BufTypeHandle bh)
  313. {
  314.     Handle        h;
  315.     char        *cp = nil;
  316.     
  317.     h = GetBuf_Buf(bh);
  318.     if (h)
  319.         cp = *h + (**bh).pos;
  320.     return cp;
  321. }
  322.  
  323. #pragma mark -
  324. //-------------------------------------------------------
  325.  
  326. void Delete_Buf(BufTypeHandle bh)
  327. {
  328.     if (!bh)
  329.         return;
  330.  
  331.     Free_Buf(bh);
  332.     DisposeHandle((Handle) bh);
  333. }
  334.  
  335. //-------------------------------------------------------
  336.  
  337. void Free_Buf(BufTypeHandle bh)
  338. {
  339.     if (!bh)
  340.         return;
  341.  
  342.     DisposeHandle((**bh).bufHandle);
  343.     Clear_Buf(bh);
  344. }
  345.  
  346. //-------------------------------------------------------
  347.  
  348. void Clear_Buf(BufTypeHandle bh)
  349. {
  350.     if (!bh)
  351.         return;
  352.  
  353.     (**bh).bufHandle = nil;
  354.     EmptyBuf_Buf(bh);
  355. }
  356.  
  357. //-------------------------------------------------------
  358.  
  359. void EmptyBuf_Buf(BufTypeHandle bh)
  360. {
  361.     if (!bh)
  362.         return;
  363.  
  364.     (**bh).pos = 0;
  365.     (**bh).len = 0;
  366. }
  367.  
  368. //-------------------------------------------------------
  369.  
  370. static Boolean ResizeBuf_Buf(BufTypeHandle bh, unsigned long newSize)
  371. {
  372.     unsigned long    oldSize = GetHandleSize((**bh).bufHandle);
  373.  
  374.     if (oldSize == newSize)
  375.         return (true);
  376.  
  377.     if ((**bh).bufHandle == nil)
  378.         (**bh).bufHandle = NewHandleClear(newSize);
  379.     else
  380.         SetHandleSize((**bh).bufHandle, newSize);
  381.  
  382.     if(MemError() != noErr)
  383.         return (false);
  384.  
  385.     if (newSize < oldSize) { /* Getting smaller */
  386.         /* Check for valid length */
  387.         if ((**bh).len > newSize)
  388.             (**bh).len = newSize;
  389.     }
  390.     return (true);
  391. }
  392.  
  393. //-------------------------------------------------------
  394.  
  395. // If doesn't continue match, returns zero
  396. // otherwise returns number of chars of hSearchBuf that have been matched
  397. unsigned long CompleteCount_Buf(BufTypeHandle buf, BufTypeHandle find,
  398.                                     unsigned long nPreMatched)
  399. {
  400.     char            *pBuf, *pBufEnd, *pFind, *pFindEnd;
  401.     unsigned long    nSkipCount = 0;
  402.  
  403.     if (IsEmpty_Buf(buf) || IsEmpty_Buf(find))
  404.         return 0;
  405.  
  406.     if (nPreMatched >= PosLen_Buf(find))
  407.         return 0;
  408.  
  409.     HLock((**buf).bufHandle);
  410.     HLock((**find).bufHandle);
  411.     pBuf = GetPos_Buf(buf);
  412.     pBufEnd = GetEnd_Buf(buf);
  413.     pFind = GetPos_Buf(find) + nPreMatched;
  414.     pFindEnd = GetEnd_Buf(find);
  415.  
  416.  
  417.     while ((pBuf <= pBufEnd) && (pFind <= pFindEnd) && ((*pBuf) == (*pFind)))
  418.         pBuf++, pFind++, nSkipCount++;
  419.  
  420.     HUnlock((**buf).bufHandle);
  421.     HUnlock((**find).bufHandle);
  422.  
  423.     if ((pBuf > pBufEnd) || (pFind > pFindEnd))
  424.         return (nPreMatched + nSkipCount);
  425.  
  426.     return 0; /* match failed */
  427. }
  428.  
  429. //-------------------------------------------------------
  430.  
  431. // Find match of hSearchBuf, either complete or end-spanning
  432. // return number of chars to skip before match
  433. // No match: return length of buf (skip the whole buffer)
  434. unsigned long SkipCount_Buf(BufTypeHandle buf, BufTypeHandle find)
  435. {
  436.     char            *pBuf, *pBufEnd;
  437.     char            *pFind, *pFindEnd;
  438.     char            *s1 = nil, *s2 = nil;
  439.     unsigned long    nSkipCount = 0;
  440.  
  441.     if (IsEmpty_Buf(buf))
  442.         return 0;
  443.  
  444.     if (IsEmpty_Buf(find))
  445.         return PosLen_Buf(buf);
  446.  
  447.     HLock((**buf).bufHandle);
  448.     HLock((**find).bufHandle);
  449.     pBuf = GetPos_Buf(buf);
  450.     pBufEnd = GetEnd_Buf(buf);
  451.     pFind = GetPos_Buf(find);
  452.     pFindEnd = GetEnd_Buf(find);
  453.  
  454.     while (pBuf <= pBufEnd) {
  455.         s1 = pBuf;
  456.         s2 = pFind;
  457.  
  458.         while ((s1 <= pBufEnd) && (s2 <= pFindEnd) && ((*s1) == (*s2)))
  459.             s1++, s2++;
  460.  
  461.         if ((s1 > pBufEnd) || (s2 > pFindEnd)) {
  462.             HUnlock((**buf).bufHandle);
  463.             HUnlock((**find).bufHandle);
  464.             return (nSkipCount);
  465.         }
  466.  
  467.         pBuf++;
  468.         nSkipCount++;
  469.     }
  470.     HUnlock((**buf).bufHandle);
  471.     HUnlock((**find).bufHandle);
  472.     return (PosLen_Buf(buf));
  473. }
  474.  
  475. //-------------------------------------------------------
  476.  
  477. Boolean Lock_Buf(BufTypeHandle bh)
  478. {
  479.     if (ValidBuf(bh)) {
  480.         HLock((**bh).bufHandle);
  481.         return true;
  482.     }
  483.     else
  484.         return false;
  485. }
  486.  
  487. //-------------------------------------------------------
  488.  
  489. void Unlock_Buf(BufTypeHandle bh)
  490. {
  491.     if (ValidBuf(bh))
  492.         HUnlock((**bh).bufHandle);
  493. }
  494.